package cn.com.czpb.client.module.personal.acticity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.ToolBarActivityWhite;
import cn.com.czpb.client.custorm.CircleImageView;
import cn.com.czpb.client.custorm.seekbar.UiSeeKBar;
import cn.com.czpb.client.model.usercenter.LevelBean;
import cn.com.czpb.client.model.usercenter.LevelEntity;
import cn.com.czpb.client.module.personal.adapter.LevelAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import cn.com.czpb.client.utils.TUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class LevelActivity extends ToolBarActivityWhite {
        @BindView(R.id.title_toolbar)
    TextView title_toolbar;
    @BindView(R.id.recycler_level)
    RecyclerView recyclerView;
    LevelAdapter adapter;
    List<LevelBean.CreditruleEntity> list;
    @BindView(R.id.seekbar_lv)
    UiSeeKBar seekbar_lv;
    @BindView(R.id.tv_need)
    TextView tv_need;
    @BindView(R.id.tv_now_lv)
    TextView tv_now_lv;
    @BindView(R.id.mylevel_activity_level)
    TextView mylevel_activity_level;
    @BindView(R.id.iv_activity_level)
    CircleImageView iv_activity_level;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        init();
        title_toolbar.setText("我的等级");
    }

    private void init() {

        list = new ArrayList<>();
        adapter = new LevelAdapter(list);
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        initList();
    }

    private void initList() {
        String uid = getIntent().getStringExtra("uid");
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        Factory.provideHttpService().MyLevel(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<LevelEntity, Boolean>() {
                    @Override
                    public Boolean call(LevelEntity levelEntity) {

                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<LevelEntity>() {
                    @Override
                    public void call(LevelEntity levelEntity) {
                        LogUtils.e("code--" + levelEntity.code);
                        if ("200".equals(levelEntity.code)) {
                            list.addAll(levelEntity.data.getCreditrule());
                            setListData(list);
                            adapter.notifyDataSetChanged();
                            int now = Integer.parseInt(levelEntity.data.getUserinfo().getExp1());
                            int need = Integer.parseInt(levelEntity.data.getUserinfo().getNeedscore());
                            int press= 100*now/(now+need);
                            LogUtils.e("code--"+press);
                            seekbar_lv.setProgress(press);
//                            seekbar_lv.setNumText(levelEntity.data.getUserinfo().getExp1());
                            seekbar_lv.setNumText("445454545");
                            tv_need.setText("升级还需 "+levelEntity.data.getUserinfo().getNeedscore()+"积分");
                            tv_now_lv.setText("Lv"+levelEntity.data.getUserinfo().getLevel());
                            mylevel_activity_level.setText("当前等级：Lv"+levelEntity.data.getUserinfo().getLevel());
                            Glide.with(activity).load(levelEntity.data.getUserinfo().getAvatar_path()).into(iv_activity_level);
                            spu.getUser().setUlevel("Lv"+levelEntity.data.getUserinfo().getLevel());
                        } else {
                            TUtils.toast(levelEntity.msg);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.e("throwable------" + throwable.getMessage());
                    }
                });
    }

    public void setListData(List<LevelBean.CreditruleEntity> list) {
        adapter.setNewData(list);
        adapter.notifyDataSetChanged();

    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_level;
    }
}
